package com.and9.mchen.mcmusic.service.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.constant.MusicManagerConstant;
import com.and9.mchen.mcmusic.dao.IAlbumDao;
import com.and9.mchen.mcmusic.dao.IImageUrlDao;
import com.and9.mchen.mcmusic.dao.ISingerDao;
import com.and9.mchen.mcmusic.dao.ISongDao;
import com.and9.mchen.mcmusic.dao.IStatisticsDao;
import com.and9.mchen.mcmusic.dao.impl.AlbumDaoImpl;
import com.and9.mchen.mcmusic.dao.impl.SongDaoImpl;
import com.and9.mchen.mcmusic.entity.Album;
import com.and9.mchen.mcmusic.entity.ImageUrl;
import com.and9.mchen.mcmusic.entity.MusicStyle;
import com.and9.mchen.mcmusic.entity.Singer;
import com.and9.mchen.mcmusic.entity.Song;
import com.and9.mchen.mcmusic.entity.Statistics;
import com.and9.mchen.mcmusic.exception.DaoException;
import com.and9.mchen.mcmusic.exception.ServiceException;
import com.and9.mchen.mcmusic.manager.DaoManager;
import com.and9.mchen.mcmusic.service.IMusicManagerService;
import com.and9.mchen.mcmusic.util.GEntityHelper;
import com.and9.mchen.mcmusic.util.MusicStyleHelper;
import com.and9.mchen.mcmusic.util.ObjectMapping;
import com.and9.mchen.mcmusic.vo.OmitAlbumBean;
import com.and9.mchen.mcmusic.vo.OmitMusicStyleBean;
import com.and9.mchen.mcmusic.vo.OmitSongBean;

public class MusicManagerServiceImpl implements IMusicManagerService{

	private static Log log=LogFactory.getLog(MusicManagerServiceImpl.class);
	private ISongDao songDao=null;
	private IAlbumDao albumDao=null;
	private IStatisticsDao statisticsDao=null;
	private ISingerDao singerDao=null;
	private IImageUrlDao imageUrlDao=null;
	
	public MusicManagerServiceImpl(){
		songDao=DaoManager.getSongDao();
		albumDao=DaoManager.getAlbumDao();
		statisticsDao=DaoManager.getStatisticsDao();
		singerDao=DaoManager.getSingerDao();
		imageUrlDao=DaoManager.getiImageUrlDao();
	}
	
	public List<OmitSongBean> getNLatestOmitSongBeans(int count) throws ServiceException {
		
		if(count <= 0){
			count=MusicManagerConstant.OMIT_SONG_PAGE_COUNT;
		}
		String sqlStr=MessageFormat.format("SELECT {0}.* FROM {0} ORDER BY {0}.issueTime DESC",
				SongDaoImpl.TABLEMANE);
		try{
			
			Object[] objects=null;
			List<Song> songs = songDao.getSongs(sqlStr, 1, count, objects);
			
			if(GEntityHelper.isEmpty(songs)){
				return null;
			}
			
			List<OmitSongBean> songBeans=new ArrayList<OmitSongBean>(songs.size());
			
			for(Song song : songs){
				
				Statistics statistics=statisticsDao.getsStatisticsBySongId(song.getId());
				
				if(statistics != null ){
					song.setStatistics(statistics);
				}
				
				song=initSingersBySong(song);
				
				OmitSongBean songBean=ObjectMapping.omitSongBeanMappingBySong(song);
				
				songBeans.add(songBean);
				
			}
			
			return songBeans;
		}catch(DaoException e){
			log.error("[MusicManagerServiceImpl: getNLatestSong] get latest songs failure", e);
			throw new ServiceException("get latest songs failure");
		}
	}

	public List<OmitAlbumBean> getNLatestOmitAlbumBeans(int count) throws ServiceException {
		if(count <= 0){
			count=MusicManagerConstant.OMIT_ALBUM_PAGE_COUNT;
		}
		
		String sqlStr=MessageFormat.format("SELECT {0}.* FROM {0} ORDER BY {0}.issueTime DESC",
				AlbumDaoImpl.TABLEMANE);
		
		try{
			
			Object[] objects=null;

			List<Album> albums= albumDao.getAlbums(sqlStr, 1, count, objects);
			
			if(GEntityHelper.isEmpty(albums)){
				return null;
			}
			
			List<OmitAlbumBean> albumBeans=new ArrayList<OmitAlbumBean>(albums.size());
			
			for(Album album : albums){
				
				Statistics statistics=statisticsDao.getStatisticsByAlbumId(album.getId());
				
				if(statistics != null){
					album.setStatistics(statistics);
				}
	
				ImageUrl imageUrl=imageUrlDao.getImageUrlByAlbumId(album.getId());

				if(imageUrl != null ){
					album.setImageUrl(imageUrl);
				}
				
				Singer singer=singerDao.getSingerByAlbumId(album.getId());
				
				if(singer != null){
					album.setSinger(singer);
				}
				
				OmitAlbumBean albumBean=ObjectMapping.omitAlbumBeanMappingByAlbum(album);
				
				albumBeans.add(albumBean);
				
			}
			
			return albumBeans;
			
		}catch(DaoException e){
			log.error("[MusicManagerServiceImpl: getNLatestAlbums] get latest albums failure", e);
			throw new ServiceException("get latest albums failure");
		}
	}

	private Song initSingersBySong(Song song) throws DaoException {
		
		List<Singer> singers=singerDao.getSingersBySongId(song.getId());
		
		if(!GEntityHelper.isEmpty(singers)){
			
			Iterator<Singer> iterator=singers.iterator();
			song.setSinger(iterator.next());
			Set<Singer> otherSingers=new HashSet<Singer>();
			
			while(iterator.hasNext()){
				otherSingers.add(iterator.next());
			}
			
			if(!GEntityHelper.isEmpty(otherSingers)){
				song.setOtherSingers(otherSingers);
			}
			
		}
		
		return song;
	}

	public List<OmitMusicStyleBean> getAllMusicStyleBeans() {
		
		List<MusicStyle> musicStyles=MusicStyleHelper.getAllMusicStyles();
		
		if(GEntityHelper.isEmpty(musicStyles)){
			return null;
		}
		
		List<OmitMusicStyleBean> musicStyleBeans=
				new ArrayList<OmitMusicStyleBean>(musicStyles.size());
		
		for(MusicStyle musicStyle : musicStyles){
			
			OmitMusicStyleBean musicStyleBean=ObjectMapping.
					omitMusicStyleBeanMappingByMusicStyle(musicStyle);
			
			musicStyleBeans.add(musicStyleBean);
		}
		
		return musicStyleBeans;
		
	}
	
}
